const pageFilter = (arr: any, ps: number = 10, pn: number = 1) => {
  ps = Number(ps);
  let pageNum = pn * 1;
  let pageSize = ps * 1;
  let total = arr.length;
  let pages = Math.ceil(total / ps);
  let start = ps * (pn - 1);
  let end = start + ps > total ? total : Number(start + ps);
  let list = [];
  for (let i = start; i < end; i++) {
    list.push(arr[i]);                                                                                                
  }
  return {
    pageNum, //当前页数
    total, //总条数
    pages, //总页数
    pageSize, //一页数据容量
    list, //一页的数据
  };
};
const newpageFilter=async(model:any,pageNumber:number,pageSize:number)=>{
  return new Promise<any>(async(resolve, reject) => {
    try {
      let total= await model.count()
      let pageNum=+pageNumber
      let pages= Math.ceil(+total / +pageSize)
      let list = await model.find().skip((+pageNumber-1)*+pageSize).limit(+pageSize);
      resolve({
        pageNum, //当前页数
        total, //总条数
        pages, //总页数
        pageSize, //一页数据容量
        list, //一页的数据
      })
    } catch (error) {
      reject(error)
      console.log('====================================');
      console.log(error);
      console.log('====================================');
    }

  })
}
export { pageFilter,newpageFilter };
